package com.showcai.controller.activity;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.module.Activity;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * Created by Baggio on 2017-11-15.
 */
@RequestMapping("/activity")
@Controller
public class ActivityController extends BaseController {
    @Autowired
    ActivityService activityService;

    /**
     * 活动列表
     *
     * @return
     */
    @RequestMapping("/activityList")
    public ModelAndView activityList() {
        ModelAndView m = new ModelAndView("/activity/activityList");
        Map<String, String> map = super.showRequestParameters();

        PageInfo<Activity> pageInfo = activityService.getActivityList(map);
        m.addObject("pageInfo", pageInfo);
        m.addObject("map", map);
        return m;
    }


    /**
     * 编辑专辑提交
     *
     * @return
     */
    @RequestMapping(value = "/activity")
    public ModelAndView activity(Long tid) {
        ModelAndView m = new ModelAndView("/activity/activity");
        if (tid != null) {
            Activity activity = activityService.getActivityById(tid);
            m.addObject("activity", activity);
        }
        return m;
    }

    /**
     * 更新活动
     *
     * @param activity
     * @return
     */
    @RequestMapping(value = "/modifyActivity")
    @ResponseBody
    public String modifyActivity(Activity activity) {
        ResultObj resultObj = new ResultObj();
       try {
           activityService.modifyActivity(activity);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

}
